package cn.hsw.manage.permission.controller;


import cn.hsw.component.core.dto.ResultDto;
import cn.hsw.component.core.util.ResultUtil;
import cn.hsw.component.web.base.BaseController;
import cn.hsw.manage.constant.ManageConstant;
import cn.hsw.manage.permission.entity.Permission;
import cn.hsw.manage.permission.service.PermissionService;
import cn.hsw.manage.permission.vo.MenuVo;
import cn.hsw.manage.permission.vo.PermissionDetailVO;
import cn.hsw.manage.permission.vo.PermissionTreeVO;
import cn.hsw.manage.permission.vo.PermissionVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李子木
 * @since 2021-11-03 08:56:37
 */
@RestController
@RequestMapping("/manage/permission")
public class PermissionController extends BaseController {

    @Autowired
    PermissionService service;

    /**
     * 权限新增
     * @param permissionVO 对象
     * @return 接口返回
     */
    @PostMapping("/add")
    @RequiresPermissions("manage:permission:add")
    public ResultDto add(@Validated PermissionVO permissionVO){

        service.add(permissionVO);

        return ResultUtil.success();
    }

    /**
     * 权限删除
     * @return 接口返回
     */
    @PostMapping("/del/{pid}")
    @RequiresPermissions("manage:permission:del")
    public ResultDto<Object> del(@PathVariable("pid")Long pid){

        service.delete(pid);

        return ResultUtil.success();
    }

    /**
     * 权限修改
     * @param permissionVO 要修改的内容
     * @return 接口返回
     */
    @PostMapping("/edit/{pid}")
    @RequiresPermissions("manage:permission:edit")
    public ResultDto<Object> edit(@PathVariable("pid")Long pid,PermissionVO permissionVO){

        service.edit(pid,permissionVO);

        return ResultUtil.success();
    }

    /**
     * 权限显示状态修改
     * @param state 1 显示 -1不显示
     * @return 接口返回
     */
    @PostMapping("/edit/state/{pid}")
    @RequiresPermissions("manage:permission:edit:state")
    public ResultDto<Object> editState(@PathVariable("pid")Long pid,@RequestParam(name = "state")Integer state){

        service.editState(pid,state);

        return ResultUtil.success();
    }


    /**
     * 权限列表树
     * @return 列表树
     */
    @PostMapping("/tree")
    @RequiresPermissions("manage:permission:tree")
    public ResultDto<List<PermissionTreeVO>> tree(@RequestParam(name = "type",defaultValue = ManageConstant.PERMISSION_TYPE_API+"")int type){

        List<PermissionTreeVO> list = service.tree(type);

        return ResultUtil.success(list);
    }
    /**
     * 权限列表树
     * @return 列表树
     */
    @PostMapping("/menu")
    @RequiresPermissions("manage:permission:menu")
    public ResultDto<MenuVo> tree(){

        MenuVo menu = service.menu();

        return ResultUtil.success(menu);
    }

    /**
     * 权限详情
     * @return 详情
     */
    @Deprecated
    @PostMapping("/info/{pid}")
    @RequiresPermissions("manage:permission:info")
    public ResultDto<PermissionDetailVO> info(@PathVariable("pid")Long pid){

        PermissionDetailVO permissionModel = service.findByPid(pid);

        return ResultUtil.success(permissionModel);
    }

}
